<?php
namespace common\libs\sms\base;
/**
 * 
 * @author fengshuang
 * 2015/6/1
 * UTF-8
 */
abstract class SmsAbstract{
	protected $config = array();
	
	/**
	 * 设置必要属性
	 * @param array $config
	 * @return Duanxin_Adapter_iHuYi|Duanxin_Adapter_DieXin
	 */
	public function setProp($config){
		$this->config = $config;
		return $this;
	}

    //解析XML
    protected static function xml_to_array($xml){
        $reg = "/<(\w+)[^>]*>([\\x00-\\xFF]*)<\\/\\1>/";
        if(preg_match_all($reg, $xml, $matches)){
            $count = count($matches[0]);
            for($i = 0; $i < $count; $i++){
                $subxml= $matches[2][$i];
                $key = $matches[1][$i];
                if(preg_match( $reg, $subxml )){
                    $arr[$key] = self::xml_to_array( $subxml );
                }else{
                    $arr[$key] = $subxml;
                }
            }
        }
        return $arr;
    }

	/**
     * 生成验证码
     */
    protected static function getCode($length = 6 , $numeric = 0)
    {
        PHP_VERSION < '4.2.0' && mt_srand((double)microtime() * 1000000);
        if($numeric) {
            $hash = sprintf('%0'.$length.'d', mt_rand(0, pow(10, $length) - 1));
        } else {
            $hash = '';
            $chars = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789abcdefghjkmnpqrstuvwxyz';
            $max = strlen($chars) - 1;
            for($i = 0; $i < $length; $i++) {
                $hash .= $chars[mt_rand(0, $max)];
            }
        }
        return $hash;
    }

    /**
     * GET 请求
     * @param string $url
     */
    protected function http_get($url){
   		 $oCurl = curl_init();
		if(stripos($url,"https://")!==FALSE){
			curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
			curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, FALSE);
			curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
		}
		curl_setopt($oCurl, CURLOPT_CONNECTTIMEOUT, 3); //timeout on connect
		curl_setopt($oCurl, CURLOPT_TIMEOUT, 3); //timeout on response
		curl_setopt($oCurl, CURLOPT_URL, $url);
		curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1 );
		$sContent = curl_exec($oCurl);
		$aStatus = curl_getinfo($oCurl);
		curl_close($oCurl);
		if(intval($aStatus["http_code"])==200){
			return $sContent;
		}else{
			return false;
		}
    }
    
    /**
     * POST 请求
     * @param string $url
     * @param array $param
     * @param boolean $post_file 是否文件上传
     * @return string content
     */
    protected function http_post($url,$param,$post_file=false){
    	$oCurl = curl_init();
    	if(stripos($url,"https://")!==FALSE){
    		curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
    		curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false);
    		curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
    	}
    	if (is_string($param) || $post_file) {
    		$strPOST = $param;
    	} else {
    		$aPOST = array();
    		foreach($param as $key=>$val){
    			$aPOST[] = $key."=".urlencode($val);
    		}
    		$strPOST =  join("&", $aPOST);
    	}
    	curl_setopt($oCurl, CURLOPT_CONNECTTIMEOUT, 3); //timeout on connect
    	curl_setopt($oCurl, CURLOPT_TIMEOUT, 3); //timeout on response
    	curl_setopt($oCurl, CURLOPT_URL, $url);
    	curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1 );
    	curl_setopt($oCurl, CURLOPT_POST,true);
    	curl_setopt($oCurl, CURLOPT_POSTFIELDS,$strPOST);
    	$sContent = curl_exec($oCurl);
    	$aStatus = curl_getinfo($oCurl);
    	curl_close($oCurl);
    	if(intval($aStatus["http_code"])==200){
    		return $sContent;
    	}else{
    		return false;
    	}
    }

}